import { useAirCityStore } from '@/stores/aircity'
import { useAnaphaseStore } from '@/stores/anaphase'
import { useWeatherStore } from '@/stores/weather'

const AirCityStore = useAirCityStore()
const AnaphaseStore = useAnaphaseStore()
const WeatherStore = useWeatherStore()

/**
 * 当前时间
 */
export const getCurrentTime = () => {
	return new Promise(async (resolve) => {
		const { hour, minute } = await __g.weather.getDateTime()
		const second = hour * 60 * 60 + minute * 60
		if (WeatherStore.currentTime === second) {
			return resolve(WeatherStore.currentTime)
		} else {
			await WeatherStore.SetCurrentTime(second)
			return resolve(second)
		}
	})
}

/**
 * 获取导览列表
 */
export const getAnimationList = () => {
	return new Promise((resolve) => {
		if (AirCityStore.animationList.length != 0) {
			return resolve(AirCityStore.animationList)
		}
		// 获取导览列表
		__g.camera.getAnimationList().then(async (animationList: any) => {
			if (!animationList.data) {
				return
			}
			const imgs = await Promise.all(animationList.data.map((item: any) => __g.camera.getAnimationImage(item.name)))
			animationList.data.forEach((item: any, index: number) => {
				item.image = (imgs[index] as any).image
			})

			AirCityStore.setAnimationList(animationList.data)
		})
		resolve(AirCityStore.animationList)
	})
}

/**
 * 获取后期对象
 */
export const getPostProcessMode = async () => {
	return new Promise(async (resolve, reject) => {
		if (Object.keys(AnaphaseStore.tonemapperOption).length == 0) {
			return resolve(AnaphaseStore.$state)
		}
		// 获取后期参数
		const {
			tonemapper,
			// 对比度
			contrast,
			// 饱和度
			saturation,
			// 泛光
			bloomIntensity,
			// 环境光遮罩
			ambientIntensity,
			// LUT效果
			lutMode,
			// 强度
			lutIntensity,
			// 滤镜效果
			postProcessEffects,
			// 线宽
			wireThickness,
			darkCorner,
			lensFlareIntensity,
			screenPercentage,
			terrainGlobalAlpha,
			terrainGlobalLitStatus,
			osgbGlobalLitStatus,
			osgbGlobalAlpha,
			antiAliasing,
			dofMode,
			receiveDecalMode,
		} = await __g.settingsPanel.getPostProcessMode()
		let bool = tonemapper != 0
		AnaphaseStore.setTonemapperOption({
			tonemapper: bool,
			// 对比度
			contrast,
			// 饱和度
			saturation,
			// 泛光
			bloomIntensity,
			// 环境光遮罩
			ambientIntensity,
			darkCorner,
			lensFlareIntensity,
			screenPercentage,
			terrainGlobalAlpha: 1,
			terrainGlobalLitStatus: true,
			osgbGlobalLitStatus: false,
			osgbGlobalAlpha: 1,
			antiAliasing: true,
			dofMode: 0,
			postProcessEffects: 0,
			receiveDecalMode,
		})
		AnaphaseStore.setLutOption({
			// LUT效果
			lutMode,
			// 强度
			lutIntensity,
		})
		AnaphaseStore.setProcessOption({
			// 滤镜效果
			postProcessEffects,
			// 线宽
			wireThickness,
		})
		return resolve(AnaphaseStore.$state)
	})
}

/**
 * 获取图层树对象
 * @returns
 */
export async function getLayerTreeObject() {
	return new Promise(async (resolve, reject) => {
		if (Object.keys(AirCityStore.layerTreeObject).length) {
			return resolve(AirCityStore.layerTreeObject)
		}

		const res: any = await __g.infoTree.get()
		// 图层树
		const layerTreeObject: any = {}
		res.infotree.forEach((item: any) => {
			layerTreeObject[item.name] = item.iD
		})
		AirCityStore.setTreeInfo(res.infotree)
		AirCityStore.setLayerTreeObject(layerTreeObject)
		return resolve(layerTreeObject)
	})
}
